package com.msb.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;

/**
 * @auther: zhaoss
 * @date: 2022\9\7 0007 - 09 - 07 - 15:23
 * @description: com.msb.common.valid
 * @version: 1.0
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private HashSet<Integer> set = new HashSet<>();

    /**
     * 初始化的方法
     * 举例：@ListValue(val={0,1})
     * 获取到 1 ，0
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] val = constraintAnnotation.val();//获得 0,1
        for (int i : val) {
            set.add(i);
        }
    }

    /**
     * 判断校验是否成功的方法
     * @param integer 客户端传递的对应的属性的值，判断value是否在0,1中
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
